package com.example.task1.ui.AllService.life;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.TakeoutFeedbackBean;
import com.example.task1.http.Api;
import com.example.task1.ui.AllService.takeout.TakeoutFeedbackAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;

public class LifeFeedbackActivity extends AppCompatActivity {

    private RecyclerView rvList;
    private Button btnAdd;
    private TakeoutFeedbackAdapter mTakeoutFeedbackAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_feedback);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("反馈");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        initView();
        initData();
        setAdapter();
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(LifeFeedbackActivity.this);
                LinearLayout linearLayout = new LinearLayout(LifeFeedbackActivity.this);
                linearLayout.setOrientation(LinearLayout.VERTICAL);
                builder.setTitle("输入反馈内容");
                EditText et_title = new EditText(LifeFeedbackActivity.this);
                et_title.setHint("标题");
                EditText et_content = new EditText(LifeFeedbackActivity.this);
                et_content.setHint("内容");
                linearLayout.addView(et_content);
                linearLayout.addView(et_title);
                builder.setView(linearLayout);
                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String s = et_title.getText().toString();
                        String s1 = et_content.getText().toString();
                        if (TextUtils.isEmpty(s) || TextUtils.isEmpty(s1)) {
                            MyApplication.show("标题或内容不能为空");
                        }else {
                            HashMap<String, Object> map = new HashMap<>();
                            map.put("title",s);
                            map.put("content",s1);
                            Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/living/feedback", Api.getBody(map));
                            Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                                @Override
                                public void success(ApiResp data) {
                                    TakeoutFeedbackBean.RowsBean rowsBean = new TakeoutFeedbackBean.RowsBean();
                                    rowsBean.setContent(s1);
                                    rowsBean.setTitle(s);
                                    List<TakeoutFeedbackBean.RowsBean> rowsBeans = Arrays.asList(rowsBean);
                                    mTakeoutFeedbackAdapter.setData(rowsBeans);
                                    MyApplication.show("反馈成功");
                                }

                                @Override
                                public void error(String msg) {

                                }
                            });
                        }
                    }
                });
                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.create().show();
            }
        });
    }

    private void setAdapter() {
        mTakeoutFeedbackAdapter = new TakeoutFeedbackAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rvList.setAdapter(mTakeoutFeedbackAdapter);
        rvList.setLayoutManager(linearLayoutManager);
    }

    private void initData() {
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/living/feedback/list");
        Api.send(apiRespCall, TakeoutFeedbackBean.class, new CallBack<TakeoutFeedbackBean>() {
            @Override
            public void success(TakeoutFeedbackBean data) {
                mTakeoutFeedbackAdapter.setData(data.getRows());
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }


    private void initView() {
        rvList = findViewById(R.id.rv_list);
        btnAdd = findViewById(R.id.btn_add);
    }
}